package 左哥算法.ch17技巧篇.打印题;

public class Ch02S型打印矩阵 {
    public static void main(String[] args) {
        int[][] matrix = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12}};
        printMatrixZigZag(matrix);
    }

    private static void printMatrixZigZag(int[][] matrix) {
        int row0=0,col0=0;
        int row1=0,col1=0;
        boolean d=true;
        while (row1<matrix.length){
            print(matrix,row0,col0,row1,col1,d);
            if (row0<matrix.length-1){
                row0++;
            }else {
                col0++;
            }
            if (col1<matrix[0].length-1){
                col1++;
            }else {
                row1++;
            }
            d=!d;
        }
    }
    private static void print(int[][] matrix,int row0,int col0,int row1,int col1, boolean d) {
        while (row0>=row1&&col0<=col1){
            if (d){
                System.out.printf(matrix[row0][col0]+" ");
                row0--;
                col0++;
            }else {
                System.out.printf(matrix[row1][col1]+" ");
                row1++;
                col1--;
            }
        }
    }
}
